package com.woate.wingframework.lang;

import java.util.Date;

/**
 * 快速日期格式化
 *
 * @author woate
 */
public class FastDateFormat implements DateFormat {
    @Override
    public String format(long millis, DateStyle dateStyle) {
        return format(new FastDate(millis), dateStyle);
    }

    @Override
    public String format(Date date, DateStyle dateStyle) {
        return format(date.getTime(), dateStyle);
    }

    @Override
    public String format(FastDate fastDate, DateStyle dateStyle) {
        //如果没有解析过，则进行解析
        if (!fastDate.bproc) {
            fastDate.proc();
        }
        if (dateStyle == DateStyle.MILLI_FORMAT1) {
            return fill(fastDate.year, 4) + "-"
                    + fill(fastDate.month, 2) + "-"
                    + fill(fastDate.day, 2) + " "
                    + fill(fastDate.h, 2) + ":"
                    + fill(fastDate.m, 2) + ":"
                    + fill(fastDate.s, 2) + ","
                    + fill(fastDate.ss, 3);
        } else if (dateStyle == DateStyle.MILLI_FORMAT2) {
            return fill(fastDate.h, 2) + ":"
                    + fill(fastDate.m, 2) + ":"
                    + fill(fastDate.s, 2) + ","
                    + fill(fastDate.ss, 3);
        } else if (dateStyle == DateStyle.SECOND_FORMAT1) {
            return fill(fastDate.year, 4) + "-"
                    + fill(fastDate.month, 2) + "-"
                    + fill(fastDate.day, 2) + " "
                    + fill(fastDate.h, 2) + ":"
                    + fill(fastDate.m, 2) + ":"
                    + fill(fastDate.s, 2);
        } else if (dateStyle == DateStyle.SECOND_FORMAT2) {
            return fill(fastDate.year, 4) + "/"
                    + fill(fastDate.month, 2) + "/"
                    + fill(fastDate.day, 2) + " "
                    + fill(fastDate.h, 2) + ":"
                    + fill(fastDate.m, 2) + ":"
                    + fill(fastDate.s, 2);
        } else if (dateStyle == DateStyle.SECOND_FORMAT3) {
            return fill(fastDate.h, 2) + ":"
                    + fill(fastDate.m, 2) + ":"
                    + fill(fastDate.s, 2);
        } else if (dateStyle == DateStyle.MINUTE_FORMAT1) {
            return fill(fastDate.year, 4) + "-"
                    + fill(fastDate.month, 2) + "-"
                    + fill(fastDate.day, 2) + " "
                    + fill(fastDate.h, 2) + ":"
                    + fill(fastDate.m, 2);
        } else if (dateStyle == DateStyle.MINUTE_FORMAT2) {
            return fill(fastDate.year, 4) + "/"
                    + fill(fastDate.month, 2) + "/"
                    + fill(fastDate.day, 2) + " "
                    + fill(fastDate.h, 2) + ":"
                    + fill(fastDate.m, 2);
        } else if (dateStyle == DateStyle.DAY_FORMAT1) {
            return fill(fastDate.year, 4) + "-"
                    + fill(fastDate.month, 2) + "-"
                    + fill(fastDate.day, 2);
        } else if (dateStyle == DateStyle.DAY_FORMAT2) {
            return fill(fastDate.year, 4) + "/"
                    + fill(fastDate.month, 2) + "/"
                    + fill(fastDate.day, 2);
        } else if (dateStyle == DateStyle.FILE_FORMAT1) {
            return fill(fastDate.year, 4) + "-"
                    + fill(fastDate.month, 2) + "-"
                    + fill(fastDate.day, 2) + "_"
                    + fill(fastDate.h, 2) + "-"
                    + fill(fastDate.m, 2) + "-"
                    + fill(fastDate.s, 2);
        } else if (dateStyle == DateStyle.FILE_FORMAT2) {
            return fill(fastDate.year, 4)
                    + fill(fastDate.month, 2)
                    + fill(fastDate.day, 2)
                    + fill(fastDate.h, 2)
                    + fill(fastDate.m, 2)
                    + fill(fastDate.s, 2);
        } else if (dateStyle == DateStyle.FILE_FORMAT3) {
            return fill(fastDate.year, 4)
                    + fill(fastDate.month, 2)
                    + fill(fastDate.day, 2)
                    + fill(fastDate.h, 2)
                    + fill(fastDate.m, 2)
                    + fill(fastDate.s, 2)
                    + fill(fastDate.ss, 3);
        } else if (dateStyle == DateStyle.FILE_FORMAT4) {
            return fill(fastDate.year, 4)
                    + fill(fastDate.month, 2)
                    + fill(fastDate.day, 2);
        } else if (dateStyle == DateStyle.FILE_FORMAT5) {
            return fill(fastDate.h, 2)
                    + fill(fastDate.m, 2)
                    + fill(fastDate.s, 2);
        } else if (dateStyle == DateStyle.FILE_FORMAT6) {
            return fill(fastDate.h, 2)
                    + fill(fastDate.m, 2)
                    + fill(fastDate.s, 2)
                    + fill(fastDate.ss, 3);
        } else if (dateStyle == DateStyle.CHINESE_FORMAT1) {
            return fill(fastDate.year, 4) + "年"
                    + fill(fastDate.month, 2) + "月"
                    + fill(fastDate.day, 2) + "日 "
                    + fill(fastDate.h, 2) + "时"
                    + fill(fastDate.m, 2) + "分"
                    + fill(fastDate.s, 2) + "秒";
        } else if (dateStyle == DateStyle.CHINESE_FORMAT2) {
            return fill(fastDate.year, 4) + "年"
                    + fill(fastDate.month, 2) + "月"
                    + fill(fastDate.day, 2) + "日";
        } else if (dateStyle == DateStyle.CHINESE_FORMAT3) {
            return fill(fastDate.h, 2) + "时"
                    + fill(fastDate.m, 2) + "分"
                    + fill(fastDate.s, 2) + "秒";
        }
        throw new IllegalArgumentException("无效的日期样式!");
    }

    /**
     * 将整数转换并且填充到指定长度的字符串
     *
     * @param in     输入整数
     * @param length 指定长度
     * @return 字符串
     */
    String fill(int in, int length) {
        if (length == 1) {
            return "" + in;
        } else if (length == 2) {
            if (in < 10) {
                return "0" + in;
            } else {
                return "" + in;
            }
        } else if (length == 3) {
            if (in < 10) {
                return "00" + in;
            } else if (in < 100) {
                return "0" + in;
            } else {
                return "" + in;
            }
        } else if (length == 4) {
            if (in < 10) {
                return "000" + in;
            } else if (in < 100) {
                return "00" + in;
            } else if (in < 1000) {
                return "0" + in;
            } else {
                return "" + in;
            }
        } else {
            return "" + in;
        }
    }
}